package com.payment.controller;
import com.payment.entity.Office;
import com.payment.entity.OfficeWithDoctorDTO;
import com.payment.service.OfficeService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/office")  //科室接口
public class OfficeController {

    @Resource
    public OfficeService officeService;

    //查询所有科室
    @GetMapping("/getAllOffice")
    public ResponseEntity<ResDTO> queryAllOffice(){
        List<Office> _list =officeService.getAll();
        if (_list!=null){
            return ResponseEntity.ok(ResDTO.success(_list));
        }
        return ResponseEntity.ok(ResDTO.error());
    }


    //查询医生所在的科室
    @GetMapping("/getAll")
    public ResponseEntity<ResDTO> queryAll(){

        List<OfficeWithDoctorDTO> _list =officeService.getOfficeWithDoctor();

        System.out.println(_list);

        if (_list!=null){
            return ResponseEntity.ok(ResDTO.success(_list));
        }else {
            return ResponseEntity.ok(ResDTO.error());
        }
    }

    @PostMapping("/addOffice")
    public ResponseEntity<ResDTO> add(@RequestBody Office entity){

        int res= officeService.add(entity);
        if (res>0){
            return  ResponseEntity.ok(ResDTO.success(null));
        }else{
            return  ResponseEntity.ok(ResDTO.error("新增失败！"));
        }
    }

    @PutMapping("/putOffice/{id}")
    public  ResponseEntity<ResDTO> update(@PathVariable int id,@RequestBody Office entity){
        entity.setId(id);  // 设置 ID
        int res = officeService.update(entity);
        if (res > 0) {
            return ResponseEntity.ok(ResDTO.success(null));
        }
        return ResponseEntity.ok(ResDTO.error("更新失败！"));
    }

    //删除
    @DeleteMapping("/delOffice/{id}")
    public ResponseEntity<ResDTO> remove(@PathVariable int id){
        int res=officeService.del(id);
        if (res!=0){
            return ResponseEntity.ok(ResDTO.success(null));
        }else {
            return ResponseEntity.ok(ResDTO.error("没有找到该用户！删除失败！"));
        }
    }

}